package chenxi.concurrent;

import com.google.common.util.concurrent.*;

import java.util.Random;
import java.util.concurrent.*;

/**
 * @author bowen
 */
public class TestListenableFuture2 {

    /**
     * 创建线程池
     */
    final static ListeningExecutorService SERVICE = MoreExecutors.listeningDecorator(Executors.newCachedThreadPool());

    public TestListenableFuture2(int executorSize) {

        ListeningExecutorService executorService = MoreExecutors.listeningDecorator(new ThreadPoolExecutor(
                executorSize, executorSize, 0, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>(), new ThreadFactoryBuilder().setDaemon(true).setNameFormat("Executor-Pool-%d").build()));
        MoreExecutors.addDelayedShutdownHook(executorService, 60, TimeUnit.SECONDS);
    }


    ExecutorService executorService = Executors.newSingleThreadExecutor();

    ExecutorService executorService1 = Executors.newFixedThreadPool(10);

    ExecutorService executorService2 = Executors.newCachedThreadPool();


    public static void main(String[] args) throws Exception {
        Long t1 = System.currentTimeMillis();
        // 任务1
        ListenableFuture<Boolean> booleanTask = SERVICE.submit(new Callable<Boolean>() {
            @Override
            public Boolean call() {
                return true;
            }
        });

        booleanTask.addListener(() -> System.out.println("tt"), SERVICE);

        Futures.addCallback(booleanTask, new FutureCallback<Boolean>() {
            @Override
            public void onSuccess(Boolean result) {
                System.err.println("BooleanTask: " + result);
            }

            @Override
            public void onFailure(Throwable t) {
            }
        }, SERVICE);

        // 任务2
        ListenableFuture<String> stringTask = SERVICE.submit(new Callable<String>() {
            @Override
            public String call() throws Exception {
                return "Hello World";
            }
        });

        Futures.addCallback(stringTask, new FutureCallback<String>() {
            @Override
            public void onSuccess(String result) {
                System.err.println("StringTask: " + result);
            }

            @Override
            public void onFailure(Throwable t) {
            }
        }, SERVICE);

        // 任务3
        ListenableFuture<Integer> integerTask = SERVICE.submit(() -> new Random().nextInt(100));

        ListenableFuture<Integer> integerListenableFuture = SERVICE.submit(Callables.returning(new Random().nextInt(100)));


        Futures.addCallback(integerTask, new FutureCallback<Integer>() {
            @Override
            public void onSuccess(Integer result) {
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.err.println("IntegerTask: " + result);
            }

            @Override
            public void onFailure(Throwable t) {
            }
        }, SERVICE);

        MoreExecutors.addDelayedShutdownHook(SERVICE, 10, TimeUnit.MILLISECONDS);

        // 执行时间
        System.err.println("time: " + (System.currentTimeMillis() - t1));


        System.out.println(String.format("Sharding-JDBC-%d"));

    }
}
